<?php

namespace backend\controllers;

use Yii;
use common\models\category\Category;
use common\models\category\CategorySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\adminlog\AdminLog;
use common\models\articles\Articles;

/**
 * CategoryController implements the CRUD actions for Category model.
 */
class CategoryController extends Controller
{


    /**
     * Lists all Category models.
     * @return mixed
     */
    public function actionIndex()
    {
        // $searchModel = new CategorySearch();
        // $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        // return $this->render('index', [
            // 'searchModel' => $searchModel,
            // 'dataProvider' => $dataProvider,
        // ]);
		$data = Category::find()->where(['parent_id'=>0])->all();
        return $this->render('index', [
            'num' => count($data),
            'data' => $data,
        ]);
    }

    /**
     * Displays a single Category model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
		$data=Category::findOne($id);
		Adminlog::addlog(yii::$app->user->id, "查看栏目 ".$data->name);
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Category model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Category();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
			$name = Yii::$app->request->post()['Category']['name'];
			Adminlog::addlog(yii::$app->user->id, "添加栏目 ".$name);
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
	
	public function actionChildren($id)
	{
		$layer=Yii::$app->request->get()['layer'];
		$children = Category::find()->where(['parent_id'=>$id])->all();
        if($children) {
            foreach($children as $k => $v) {
                $haschildren = Category::find()->where(['parent_id'=>$v['id']])->count();
				$str = (category::isDel($v['id'])) ? "该栏目下有子栏目或者有内容,不能删除！" : "你确定要删除该栏目吗？";
                $result[$k] = array(
                    'haschildren' => $haschildren,
                    'id'          => $v['id'],
                    'parent_id'   => $v['parent_id'],
                    'name'        => $v['name'],
                    'status'      => $v['status'],
                    'url'         => $v['url'],
                    'sort'        => $v['sort'],
                    'keywords'    => $v['seo_keywords'],
                    'contents'    => $v['seo_contents'],
					'deltrue'     => $str,
					'layer'       => $layer+1,
                    'view_url'    =>  \yii\helpers\Url::to(['category/view','id'=>$v['id']],true),
                    'update_url'  =>  \yii\helpers\Url::to(['category/update','id'=>$v['id']],true),
                    'delete_url'  =>  \yii\helpers\Url::to(['category/delete','id'=>$v['id']],true),
                );
            }
        } else {
            $result = array();
        }
        exit(json_encode($result));
	}
	
	public function actionGetchildren($id)
	{
		$result = Category::get($id, Category::find()->all());
        exit(json_encode($result));
	}

    /**
     * Updates an existing Category model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
			Adminlog::addlog(yii::$app->user->id, "修改栏目 ".$model->name);
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Category model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
		Adminlog::addlog(yii::$app->user->id, "删除栏目 ".$model->name);
		$model->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the Category model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Category the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Category::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
	
	/**
     * @inheritdoc
     */
}
